package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"doctor-medicine-system/pkg"

	"doctor-medicine-system/docrpc/docrpc"
	"doctor-medicine-system/docrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *docrpc.LoginRequest) (*docrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var doctoc mysql.DocToc
	doctoc = mysql.DocToc{
		Model:      gorm.Model{},
		DocTocCode: in.DocTocCode,
		Password:   pkg.Md5(in.Password),
	}

	if err := doctoc.FindOrCreate(mysql.DB, in.DocTocCode); err != nil {
		return nil, err
	}
	return &docrpc.LoginResponse{
		DocTocId: int64(doctoc.ID),
	}, nil
}
